#!/usr/bin/perl
#mdmgroups-lib.pl

do "../web-lib.pl";
&init_config();
%access = &get_module_acl();

#get_grpinfo()
sub get_groupinfo()
{
	my (@file,$groupname,%group);	
	
	open(FILE,$config{'groupfile'});
	@file=<FILE>;
	close(FILE);
	
	foreach(@file){
		chomp;
		if (/^[\S]/){
			$groupname = $_;
			$groupname =~ s/:.*//;
			if ($group{'[group]'} eq ''){
				$group{'[group]'}=$groupname;
			}else{
				$group{'[group]'}=$group{'[group]'}.",".$groupname;			
			}
			s/^.*://;
			$group{"$groupname"}=$_;
		}
	}
	
	return (\%group);
}

#display_5_members($strmembers)
sub display_3_members()
{
	my ($strmembers, $i, @line);

	$strmembers = @_[0];

	@line = split(",", $strmembers);
	if(@line < 4)
	{
		return join(";   ",@line);
		#return $strmembers;
	}
	return join(";   ", $line[0], $line[1], $line[2]."...");
}

#sub save_new_grp($newname,$memlist);
sub save_new_grp()
{
	my $file;	
	
	$file="$_[0]:$_[1]\n";
	open(FILE,">>$config{'groupfile'}");
	print FILE $file;
	close(FILE);
	
	&save_hostgrp("##",$_[0],$_[1]);	
}

#sub save_new_grp($groupname,$newname,$memlist);
sub save_old_grp()
{
	my @file;	

	open(FILE,"$config{'groupfile'}");
	@file=<FILE>;
	close(FILE);	

	for ($i=0;$i<@file;$i++){
		if ($file[$i] =~ /^$_[0]:/){
			$file[$i]="$_[1]:$_[2]\n";
			last;
		}
	}

	open(FILE,">$config{'groupfile'}");
	print FILE @file;
	close(FILE);
	
	&save_hostgrp($_[0],$_[1],$_[2]);
}
#delete_grp($groupname)
sub delete_grp()
{
	my @file;	

	open(FILE,"$config{'groupfile'}");
	@file=<FILE>;
	close(FILE);	

	for ($i=0;$i<@file;$i++){
		if ($file[$i] =~ /^$_[0]:/){
			$file[$i]="";
			last;
		}
	}

	open(FILE,">$config{'groupfile'}");
	print FILE @file;
	close(FILE);
	
	&del_hostgrp($_[0]);
}

#save_hostgrp($oldgrpname,$grpname,$hostname)
#if $oldgrpname is "##", it means this is a new grpname
sub save_hostgrp()
{
	my $grpname=$_[1];
	my (@hostid1,@hostid2,$hostid);
	my $f;
	my $filename;
	
	if ($_[0] ne "##"){
		&del_hostgrp($_[0]);		
	}

	@hostid1=split(/,/,$_[2]);
	foreach (@hostid1){
		if (/\[This server\]/){
			next;
		}
		$_ =~ s/\[//;
		$_ =~ s/\].*//;
		push @hostid2,$_;
	}
	
	opendir(DIR, $config{'searchdir'});
	while($f = readdir(DIR)) {
		foreach $hostid (@hostid2){
			$filename =$hostid.".serv";
			if ($f =~ /$filename/) {
				&save_grpinfo($grpname,$hostid);
			}
		}
	}
	closedir(DIR);	
}

#del_hostgrp($grpname)
sub del_hostgrp()
{
	my $grpname=$_[0];
	
	opendir(DIR, $config{'searchdir'});
	while($f = readdir(DIR)) {
		if ($f =~ /\.serv$/) {
			&del_grpinfo($grpname,$f);
		}
	}
	closedir(DIR);		
	
}

#save_grpinfo($grpname,$hostname)
sub save_grpinfo(){
	local $filename=$config{'searchdir'}.$_[1].".serv";
	local (@name,$name);
	open(FILE,$filename); 
	local @file=<FILE>;
	close(FILE);

	for ($i=0;$i<@file;$i++){
		if ($file[$i] =~ /^group=/){
			$name=$file[$i];
			chomp($name);
			$name=~ s/^group=//;
			@name = split(/,/,$name);
			foreach $n (@name){
				if ($n eq $_[0]){
					return 1;
				}
			}
			if ($name eq ""){
				$name = $_[0];
			}else{
				$name = $name.",".$_[0];
			}
			$file[$i] ="group=$name\n";
		}
	}

	open(FILE,">$filename");
	print FILE @file;
	close(FILE);
}


#del_grpinfo($grpname,$hostname)
sub del_grpinfo(){
	local $filename=$config{'searchdir'}.$_[1];
	local (@name,@newname,$name);
	open(FILE,$filename); 
	local @file=<FILE>;
	close(FILE);

	for ($i=0;$i<@file;$i++){
		if ($file[$i] =~ /^group=/){
			$name=$file[$i];
			chomp($name);
			$name=~ s/^group=//;
			@name = split(/,/,$name);
			foreach $n (@name){
				if ($n ne $_[0]){
					push @newname,$n;
				}
			}
			$name = join (",",@newname);
			$file[$i] ="group=$name\n";
		}
	}

	open(FILE,">$filename");
	print FILE @file;
	close(FILE);
}

sub refresh_grpinfo()
{
	&foreign_require("mdmsearch","mdmsearch-lib.pl");
	local @devlist=&foreign_call("mdmsearch","list_servers");
	local ($line,$line2);
	local $grp;
	local @grpinfo;
	local $find;
	
	open(FILE,"$config{'groupfile'}");
	@file=<FILE>;
	close(FILE);	

	for ($i=0;$i<@file;$i++){
		local (@dev,@dev2);
		$line=$file[$i];
		chomp($line);
		$grp=$line;
		$grp=~ s/:.*$//;
		$line=~ s/^.*://;
		@dev=split(/,/,$line);
		foreach $d (@dev){
			if ($d =~ /\[This server\]/){
				push (@dev2,"[This server]");
				next;
			}
			$find = 0;
			$d2=$d;
			$d =~ s/^\[//;
			$d =~ s/\].*//;
			foreach(@devlist){
				if ($_->{'id'} eq $d){
					$line2 = "[".$d."]".$_->{'ip'};
					push (@dev2,$line2);
					$find = 1;
					last;
				}
			}
			#if($find == 0){
			#	push (@dev2,$d2);
			#}
		}

		local $line2 = join(",",@dev2);
		$line2 = $grp.":".$line2."\n";	
		push @grpinfo,$line2;
	}

	open(FILE,">$config{'groupfile'}");
	print FILE @grpinfo;
	close(FILE);	
}

1;



